home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d3 / rettig.arc / TRSOURCE.EXE / LINES.C < prev    next >
C/C++ Source or Header  |  1990-10-22  |  2KB  |  65 lines

  1. /*********
  2. *
  3. * LINES.C
  4. *
  5. * by Tom Rettig
  6. * modified by Leonard Zerman
  7. *
  8. * Placed in the public domain by Tom Rettig Associates, 10/22/1990.
  9. *
  10. *  Syntax: LINES( <expC>, <line len> )
  11. *  Return: <expN> number of lines of length <line len> in <expC>.
  12. *          Zero if <expC> is null, if <line len> is <= 0,
  13. *             or if <line len> is too small to handle an unbroken word.
  14. *********/
  15.  
  16. #include "trlib.h"
  17.  
  18. TRTYPE lines()
  19. {
  20.    char *instr;
  21.    int i, j, len, maxline, ret;
  22.  
  23.    if ( PCOUNT==2 && ISCHAR(1) && ISNUM(2) )
  24.    {
  25.       instr   = _parc(1);
  26.       maxline = _parni(2);
  27.       len     = _tr_strlen(instr);
  28.  
  29.       /* Return error if negative or zero line length or an empty string */
  30.       if ( maxline <= 0 || len < 1 )
  31.          _retni( ERROR );
  32.       else
  33.       {
  34.          /* Repeat until all full lines have been counted */
  35.          for ( i=maxline-1, j=0, ret=1; i+maxline <= len; i+=maxline )
  36.          {
  37.             /* Back up to first space if in middle of a word */
  38.             while ( instr[i] != SPACEC && instr[i+1] != SPACEC )
  39.                i--;
  40.  
  41.             if ( i >= j )
  42.             {
  43.                /* move head pointer and increment line counter */
  44.                j = i + 1;
  45.                ret++;
  46.             }
  47.             else
  48.             {
  49.                /* error if word is too long to fit in maxline */
  50.                _retni( ERROR );
  51.                return;
  52.             }
  53.          }
  54.          /* add one for last line if any characters left over */
  55.          if ( (i+1) < len  )
  56.             ret ++;
  57.  
  58.          /* return the number of lines */
  59.          _retni( ret );
  60.       }
  61.    }
  62.    else
  63.       _retni( ERROR );
  64. }
  65.